package org.lilystudio.smarty4j.statement.modifier;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.lilystudio.smarty4j.Context;
import org.lilystudio.smarty4j.expression.StringExpression;
import org.lilystudio.smarty4j.statement.Modifier;
import org.lilystudio.smarty4j.statement.ParameterCharacter;

public class $format extends Modifier {
	  private static ParameterCharacter[] definitions = { new ParameterCharacter(
		      ParameterCharacter.OBJECT, new StringExpression("")) };

	
	public Object execute(Object content, Context context, Object[] args) {
		if(content == null || args.length == 0) return null;
		String format = (String)args[0];
		if(content instanceof Date) {
			var df = new SimpleDateFormat(format);
			var result = df.format((Date)content);
			return result;
		} else if(content instanceof Number) {
			var formatter = new DecimalFormat(format);
			var result = formatter.format((Number)content);
			return result;
		}
		return content;
	}

	
	public ParameterCharacter[] getDefinitions() {
		return definitions;
	}
}
